package com.taokatao.cardmee.api;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.taokatao.cardmee.App;
import com.taokatao.cardmee.C;
import com.taokatao.cardmee.Utils;
import com.taokatao.cardmee.common.widget.ProgressLoadingDialog;

import in.srain.cube.views.ptr.PtrFrameLayout;
import rx.Subscriber;

@SuppressWarnings({"unused"})
public abstract class SubscriberAdapter<T> extends Subscriber<T> {
    private final Handler delayHandler = new Handler(Looper.getMainLooper());
    private Runnable runProgress;
    private ProgressLoadingDialog d;
    private boolean hasErrorToast = true;

    private PtrFrameLayout layPtrFrame;
    private ImageView iLoad;
    private View bSubmit;

    public SubscriberAdapter<T> errorToast(boolean errorToast) {
        hasErrorToast = errorToast;
        return this;
    }

    public SubscriberAdapter<T> showProgress(final Context context) {
        if (d == null) {
            delayHandler.postDelayed(runProgress = new Runnable() {
                @Override
                public void run() {
                    if (!((Activity) context).isFinishing()) {
                        d = new ProgressLoadingDialog.Builder(context).setTitle("请稍候...").setCancelable(false).create();
                        d.show();
                    }
                }
            }, 400);
        }
        return this;
    }

    public SubscriberAdapter<T> pullToRefresh(PtrFrameLayout ptrFrameLayout) {
        layPtrFrame = ptrFrameLayout;
        return this;
    }

    public SubscriberAdapter<T> loadingBackground(ImageView viewLoading) {
        iLoad = viewLoading;
        return this;
    }

    public SubscriberAdapter<T> submitDisable(View btnSubmit) {
        bSubmit = btnSubmit;
        return this;
    }

    @Override
    public void onNext(T o) {
        if (o instanceof RespWrapper) {
            RespWrapper t = (RespWrapper) o;
            if (C.http.RESULT_CODE_SUCCESS != t.code) {
                if (!TextUtils.isEmpty(t.message))
                    Utils.showToast(t.message);
            } else {
                if (App.DEBUG_LOG) Utils.logd("OkHttp onNext : " + ApiManager.i().decrypt(t.data));
            }
        } else {
            if (App.DEBUG_LOG) Utils.logd("OkHttp onNext : " + App.i().getGson().toJson(o));
        }
    }

    @Override
    public void onCompleted() {
        if (runProgress != null) {
            delayHandler.removeCallbacks(runProgress);
            runProgress = null;
        }
        if (d != null && d.isShowing()) {
            d.dismiss();
            d = null;
        }
        if (layPtrFrame != null) {
            layPtrFrame.refreshComplete();
        }
        if (iLoad != null && iLoad.getBackground() != null
                && iLoad.getBackground() instanceof AnimationDrawable) {
            ((AnimationDrawable) iLoad.getBackground()).stop();
            iLoad.setVisibility(View.GONE);
        }
        if (bSubmit != null) {
            bSubmit.setEnabled(true);
        }
    }

    @Override
    public void onError(Throwable e) {
        e.printStackTrace();

        if (hasErrorToast) {
            Utils.showToast(Utils.checkNet() ? "服务器异常，请稍后重试" : "世界上最遥远的距离就是没网");
        }
        if (runProgress != null) {
            delayHandler.removeCallbacks(runProgress);
            runProgress = null;
        }
        if (d != null && d.isShowing()) {
            d.dismiss();
            d = null;
        }
        if (layPtrFrame != null) {
            layPtrFrame.refreshComplete();
        }
        if (iLoad != null && iLoad.getBackground() != null
                && iLoad.getBackground() instanceof AnimationDrawable) {
            ((AnimationDrawable) iLoad.getBackground()).stop();
            iLoad.setVisibility(View.GONE);
        }
        if (bSubmit != null) {
            bSubmit.setEnabled(true);
        }
    }
}
